package basicprocess.inheritance;

import java.util.Hashtable;
import java.util.LinkedList;
import java.util.Vector;

import dbmanager.DBManager;
import dbmanager.GetIdName;

import utility.CapitalChar;
import utility.ConvertToLower;

public class Scope 
{
	DBManager database;
	GetIdName gid;
	String SCOPE="scope";
	String INHERITANCE="inheritance";
	public void setDbmanager(DBManager database)
	{
		this.database = database;
		gid = new GetIdName(database); 
		SCOPE = gid.getId(SCOPE); 
		INHERITANCE = gid.getId(INHERITANCE);
	}
	
	public Vector<Object> checkforscope(Vector<Object> vector,String ParentObject) 
	{
		LinkedList<Object> linkedlist = new LinkedList<Object>();
		for(int i=0;i<vector.size();i++)
		{
			Hashtable<Object, Object>table =(Hashtable<Object, Object>) vector.get(i);
			table = ConvertToLower.convertHashKey(table);
			//String splitvector[] = vector.get(i).toString().split("\t");
			
		
			if(table.get("pid").equals(SCOPE))
			{
				String scopvalue = table.get("pv").toString();
				if(!linkedlist.contains(scopvalue))
					linkedlist.add(scopvalue);
			}
		}
		for(int i=0;i<linkedlist.size();i++)
		{	 
			 try 
			 {
				 scope.Scope scope;
				 Class C = Class.forName("scope."+CapitalChar.makeFirstCharCapital(gid.getItem(linkedlist.get(i).toString())));
				 scope  =(scope.Scope)C.newInstance();
				 
				 scope.setDbmanger(database);
				 scope.setVector(vector);
				 scope.getParent(ParentObject);
				 scope.processScope();
				 vector = scope.getVector();
			 }
			 catch (ClassNotFoundException e) 
			 {
				e.printStackTrace();
			 }
			 catch (InstantiationException e) 
			 {
				e.printStackTrace();
			 }
			 catch (IllegalAccessException e) 
			 {
				e.printStackTrace();
			 }
		}
		for(int i=0;i<vector.size();i++)
		{
			Hashtable<Object, Object>  table = new Hashtable<Object, Object>();
			table =(Hashtable<Object, Object>) vector.get(i);
			if(table.get("pid").equals(INHERITANCE))
				vector.remove(i);
		}
		return vector;
	}
}
